package org.smart4j.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 流操作工具类
 *
 * @author YooLin1c
 * @version 1.0.0
 * @since 2018-01-30 20:45
 */
@Slf4j
public final class StreamUtil {
    /**
     * 以字符串方式读取流中数据
     *
     * @param inputStream 输入流
     * @return 字符串
     */
    public static String getString(ServletInputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            log.error("read string from inputStream failed!", e);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }
}
